package com.mysoft.gfyx.workbench.controller;

import com.mysoft.framework.modeling.dataapi.DataApi;
import com.mysoft.framework.mvc.api.Controller;
import com.mysoft.framework.rpc.annotation.PubAction;
import com.mysoft.framework.rpc.annotation.PubService;
import com.mysoft.framework.rpc.contants.RequestPrefix;
import com.mysoft.gfyx.workbench.controller.dto.NewTjProfitRequestDto;
import com.mysoft.gfyx.workbench.dao.entity.PermTenantProfit;
import com.mysoft.gfyx.workbench.service.ProfitProvinceService;
import com.mysoft.gfyx.workbench.service.ProfitService;
import com.mysoft.gfyx.workbench.service.dto.CheckProfitRequestDto;
import com.mysoft.gfyx.workbench.service.dto.GetProfitProvinceDataDto;
import com.mysoft.gfyx.workbench.service.dto.GetProfitsResponseDto;
import com.mysoft.gfyx.workbench.service.dto.SaveProfitProvinceDto;
import com.mysoft.gfyx.workbench.service.exception.InitTenantException;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.LinkedList;
import java.util.List;

@PubService(value = "/profitPub", prefix = RequestPrefix.PUB, method = RequestMethod.POST)
@Tag(name = "权益控制器-Pub")
public class ProfitPubController extends Controller implements DataApi {

    @Autowired
    private ProfitProvinceService profitProvinceService;
    @Autowired
    private ProfitService profitService;


    @PubAction(value = "/initTenant", method = RequestMethod.POST)
    public boolean initTenant(@RequestBody NewTjProfitRequestDto newTjProfitRequestDto) {
        try {
            profitService.initTenant(newTjProfitRequestDto);
        } catch (InitTenantException e) {
            List<String> errors = new LinkedList<>();
            errors.add(e.getMessage());
            profitService.logInitiation(newTjProfitRequestDto, errors, null);
        }

        return true;
    }

    @PubAction(value = "/getProfitProvince", method = RequestMethod.POST)
    public GetProfitProvinceDataDto getProfitProvince() {
        return profitProvinceService.getProfitProvince();
    }

    @PubAction(value = "/saveProfitProvince", method = RequestMethod.POST)
    public void saveProfitProvince(@RequestBody SaveProfitProvinceDto dto) {
        profitProvinceService.saveProfitProvince(dto);
    }

    @PubAction(value = "/checkProfitEffectByCode", method = RequestMethod.POST)
    public Boolean checkProfitEffectByCode(@RequestBody CheckProfitRequestDto dto) {
        return profitService.checkProductCodeEffective(dto.getCode());
    }

    @PubAction(value = "/pushProfit", method = RequestMethod.POST)
    public Boolean pushProfit() throws Exception {
        return profitService.PushProfitToMiniProgram();
    }

    @PubAction(value = "/pullYcgMemberProfit", method = RequestMethod.POST)
    public Boolean pullYcgMemberProfit() throws Exception {
        return profitService.pullYcgMemberProfit();
    }

    @PubAction(value = "/getProfits", method = RequestMethod.POST)
    public GetProfitsResponseDto getProfits() {
        GetProfitsResponseDto responseDto = new GetProfitsResponseDto();
        responseDto.setHasClueRadarProfit(false);
        responseDto.setHasMarketReportProfit(false);
        List<PermTenantProfit> list = profitService.getProfits();
        for (PermTenantProfit item : list) {
            if (item != null) {
                if (item.getProductCode().equals(ProfitService.Product.MarketReport.toString())) {
                    responseDto.setHasMarketReportProfit(true);
                } else if (item.getProductCode().equals(ProfitService.Product.ClueRadarNationwide.toString()) ||
                        item.getProductCode().equals(ProfitService.Product.ClueRadarProvince.toString())) {
                    responseDto.setHasClueRadarProfit(true);
                }
            }
        }
        return responseDto;
    }
}